package com.crud.web; /**
 * @author wzm
 * @create 2023-05-08 16:07
 */

import com.crud.bean.Employee;
import com.crud.service.EmployeeService;
import com.crud.service.impl.EmployeeServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/selectById")
public class BeforeServlet extends HttpServlet {
    private EmployeeService employeeService = new EmployeeServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //查询修改前的信息
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //获取id
        String id = request.getParameter("id");

        Employee employee = new Employee();
        employee.setId(Integer.parseInt(id));

        Employee byId = employeeService.findById(employee);

        if(byId != null){
            //查询到了数据 携带数据显示到编辑页面上
            request.setAttribute("employee",byId);
            request.getRequestDispatcher("/employee_update.jsp").forward(request,response);
        }else{
            //数据同步错误
            response.getWriter().write("数据同步错误！！！！");
        }


    }
}
